#coding:utf-8
'''
    基于Sequential建立神经网络
'''

import tensorflow as tf
from tensorflow import keras

from tensorflow.keras import layers
def exam1():
    '''
    units: 当前层中包含的神经元个数
    Activation: 激活函数，relu,sigmoid等
    use_bias: 是否使⽤偏置，默认使⽤偏置
    Kernel_initializer: 权重的初始化⽅式，默认是Xavier初始化
    bias_initializer: 偏置的初始化⽅式，默认为0
    '''
    model = keras.Sequential([
        layers.Dense(3, activation='relu',kernel_initializer='he_normal',name='dense_1',input_shape=(4,)),
        layers.Dense(2, activation='relu',kernel_initializer='he_normal',name='dense_2'),
        layers.Dense(1, activation='sigmoid',kernel_initializer='he_normal',name='dense_3')
    ],name='my_sequential_model')
    model.summary()

def exam2():
    inputs = tf.keras.Input(shape=(3,),name='input')
    x = tf.keras.layers.Dense(3,activation='relu',kernel_initializer='he_normal',name='dense_1')(inputs)
    x = tf.keras.layers.Dense(2,activation='relu',kernel_initializer='he_normal',name='dense_2')(x)
    outputs = tf.keras.layers.Dense(1,activation='sigmoid',kernel_initializer='he_normal',name='dense_3')(x)
    model = tf.keras.Model(inputs=inputs,outputs=outputs)
    model.summary()
    keras.utils.plot_model(model,show_shapes=True)

def exam3():
    class Mymodel(tf.keras.Model):
        def __init__(self):
            super(Mymodel,self).__init__()
            self.dense_1 = tf.keras.layers.Dense(3,activation='relu',kernel_initializer='he_normal',name='dense_1')
            self.dense_2 = tf.keras.layers.Dense(2,activation='relu',kernel_initializer='he_normal',name='dense_2')
            self.dense_3 = tf.keras.layers.Dense(1,activation='sigmoid',kernel_initializer='he_normal',name='dense_3')

        def call(self,inputs):
            x = self.dense_1(inputs)
            x = self.dense_2(x)
            return self.dense_3(x)

    model = Mymodel()
    x = tf.ones((1,3))
    _ = model(x)
    model.summary()
    y = model(x)
    print(y)

if __name__ == '__main__':
    #exam1()
    #exam2()
    exam3()